/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.blackup.action;

import br.com.barrientosjunior.blackup.MainManager;
import br.com.barrientosjunior.blackup.model.File;
import br.com.barrientosjunior.utils.Files;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 * Ação de backups
 * @author Junior
 */
public class ExecuteSimpleBackupAction implements BackupAction, Runnable{
    MainManager manager = null;
    File file = null;

    public ExecuteSimpleBackupAction(MainManager manager, File file) {
        this.manager = manager;
        this.file = file;
    }

    public void execute() throws IOException {
        java.io.File originFile = new java.io.File(file.getPathOrigin());
        java.io.File destinFile = new java.io.File(file.getPathDestiny());
        // TODO checar se haverá cópia
        if (originFile.exists() || originFile.lastModified() != file.getLastModified()){
            Files.copyFile(originFile, destinFile);
        }
    }

    public void run() {
        try {
            this.execute();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(manager.getMainFrame(), ex.getMessage(), "Erro de backup", JOptionPane.ERROR_MESSAGE);
        }
    }
}
